package com.study.boot.activiti.controller;

import com.study.boot.activiti.domain.Leave;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 请假控制器
 *
 * @author TangFD@HF 2018/5/2
 */
@Controller
public class LeaveController {
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;

    @GetMapping("leave")
    public String leave() {
        return "leave";
    }

    @PostMapping("/add")
    public String add(@Valid Leave leave) {
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        ProcessDefinition processDefinition = processDefinitionQuery.processDefinitionKey("leave").singleResult();
        Map<String, Object> params = new HashMap<>(2);
        params.put("leaveParam", leave);
        params.put("userId", "userId");
        identityService.setAuthenticatedUserId("userId");
        runtimeService.startProcessInstanceByKey(processDefinition.getKey(), params);
        return "redirect:tasking";
    }
}
